<template>
  <Head />
  <div id="details">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>{{ goodInfo.product_name }}</p>
        <div class="list">
          <ul>
            <li>概述</li>
            <li>参数</li>
            <li>用户评价</li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <!-- 左侧商品轮播图 -->
      <div class="block text-center">
        <span class="demonstration"></span>
        <swiper :height="560" :width="560" :data="imgs" />
      </div>
      <!-- 左侧商品轮播图END -->

      <!-- 右侧内容区 -->
      <div class="content">
        <h1 class="name">{{ goodInfo.product_name }}</h1>
        <p class="intro">
          {{ goodInfo.product_intro }}
        </p>
        <p class="store">小米自营</p>
        <div class="price">
          <span>{{ goodInfo.product_price }}</span>
          <!-- <span class="del">元</span> -->
        </div>
        <div class="pro-list">
          <span class="pro-name"></span>
          <span class="pro-price">
            <!-- <span>元</span> -->
            <!-- <span class="pro-del">元</span> -->
          </span>
          <p class="price-sum">总计 : {{ goodInfo.product_selling_price }}元</p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <p class="shop-cart" @click="addToShopping">加入购物车</p>
          <p class="like" @click="addToCollect">喜欢</p>
        </div>
        <!-- 内容区底部按钮END -->
        <div class="pro-policy">
          <ul>
            <li>
              <i></i>
              小米自营
            </li>
            <li>
              <i></i>
              小米发货
            </li>
            <li>
              <i></i>
              7天无理由退货
            </li>
            <li>
              <i></i>
              7天价格保护
            </li>
          </ul>
        </div>
      </div>
      <!-- 右侧内容区END -->
    </div>
    <!-- 主要内容END -->
  </div>
  <Footer />
</template>

<script lang="ts">
export default {
  name: "detail",
};
</script>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { useAppStore } from "../store/user";
import {
  goodsDetail,
  goodsImg,
  shoppingCart,
  addCollect,
} from "../api/detaliApi";
import { useRoute } from "vue-router";
const route = useRoute();
const imgs = ref<Array<any>>();
onMounted(async () => {
  //上来就先请求数据
  const prod_id = route.query.id;
  //获取商品详情
  const res = await goodsDetail(prod_id as string);
  goodInfo.product_id = res.Product[0].product_id;
  goodInfo.product_price = res.Product[0].product_price;
  goodInfo.product_name = res.Product[0].product_name;
  goodInfo.product_picture = res.Product[0].product_picture;
  goodInfo.product_intro = res.Product[0].product_intro;
  //获取轮播图图片
  const imgRes = await goodsImg(prod_id);
  imgs.value = imgRes.ProductPicture.map((item: any) => {
    return {
      id: item.id,
      imgPath: "http://101.132.181.9:3000/" + item.product_picture,
    };
  });
  // });
});
const appStore = useAppStore();
//
interface IGoodInfo {
  product_id: number | null;
  product_price: string;
  product_name: string;
  product_picture: string;
  product_intro: string;
  product_selling_price?: string;
}
const goodInfo = reactive<IGoodInfo>({
  product_id: null,
  product_price: "",
  product_name: "",
  product_picture: "",
  product_intro: "",
});
// 点击喜欢
const addToCollect = () => {
  // 后面这里换pinia中user_id
  addCollect(appStore.user_id, goodInfo.product_id);
};
// 加入购物车
const addToShopping = () => {
  shoppingCart(appStore.user_id, goodInfo.product_id);
};
//   addCollect(appStore.user_id, goodInfo.product_id)
//   router.push('/collect')
// }
// 加入购物车
// const toCard = () => {
//   shoppingCart(123, goodInfo.product_id)
// }
// const toCard = () => {
//   shoppingCart(appStore.user_id, goodInfo.product_id).then((res) => {
//     console.log('res', res)
//   })
// }
</script>

<style scoped>
/* 头部CSS */
#details {
  background-color: #fff;
}
.page-header {
  height: 64px;
  /* margin-top: -20px; */
  z-index: 4;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
  box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
}
#details .page-header .title {
  width: 1225px;
  height: 64px;
  line-height: 64px;
  font-size: 18px;
  font-weight: 400;
  color: #212121;
  margin: 0 auto;
}
#details .page-header .title p {
  float: left;
}
#details .page-header .title .list {
  height: 64px;
  float: right;
}
#details .page-header .title .list li {
  float: left;
  margin-left: 20px;
}
#details .page-header .title .list li a {
  font-size: 14px;
  color: #616161;
}
#details .page-header .title .list li a:hover {
  font-size: 14px;
  color: #ff6700;
}
/* 头部CSS END */

/* 主要内容CSS */
.main {
  width: 1225px;
  height: 560px;
  padding-top: 30px;
  margin: 0 auto;
  background-color: #fff;
}
.main .block {
  float: left;
  width: 560px;
  height: 560px;
}
/* #details {
  background-color: rgba(163, 163, 163, 0.8);
} */
#details .main .content {
  float: left;
  margin-left: 25px;
  width: 640px;
}
#details .main .content .name {
  height: 30px;
  line-height: 30px;
  font-size: 24px;
  font-weight: normal;
  color: #212121;
}
#details .main .content .intro {
  color: #b0b0b0;
  padding-top: 10px;
}
#details .main .content .store {
  color: #ff6700;
  padding-top: 10px;
}
#details .main .content .price {
  display: block;
  font-size: 18px;
  color: #ff6700;
  border-bottom: 1px solid #e0e0e0;
  padding: 25px 0 25px;
}
#details .main .content .price .del {
  font-size: 14px;
  margin-left: 10px;
  color: #b0b0b0;
  text-decoration: line-through;
}
#details .main .content .pro-list {
  background: #f9f9fa;
  padding: 30px 60px;
  margin: 50px 0 50px;
}
#details .main .content .pro-list span {
  line-height: 30px;
  color: #616161;
}
#details .main .content .pro-list .pro-price {
  float: right;
}
#details .main .content .pro-list .pro-price .pro-del {
  margin-left: 10px;
  text-decoration: line-through;
}
#details .main .content .pro-list .price-sum {
  color: #ff6700;
  font-size: 24px;
  padding-top: 20px;
}
.button {
  display: flex;
  height: 55px;
  margin: 10px 0 20px 0;
}
#details .main .content .button .el-button {
  float: left;
  height: 55px;
  font-size: 16px;
  color: #fff;
  border: none;
  text-align: center;
}
.shop-cart {
  width: 340px;
  background-color: #ff6700;
  line-height: 50px;
  text-align: center;
  color: white;
  border-radius: 5px;
  cursor: pointer;
}
.shop-cart:hover {
  background-color: #f25807;
}

.like {
  width: 260px;
  margin-left: 40px;
  background-color: #b0b0b0;
  text-align: center;
  line-height: 50px;
  color: white;
  border-radius: 5px;
  cursor: pointer;
}
#details .main .content .button .like:hover {
  background-color: #757575;
}
#details .main .content .pro-policy li {
  float: left;
  margin-right: 20px;
  color: #b0b0b0;
}
/* 主要内容CSS END */
</style>
